﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Data;
using System.IO;
using SaturniLibrary.Office;
using SaturniLibrary.Utility;
using Eleanor.BLL;
using SaturniLibrary.Net;
using System.Drawing;

namespace Eleanor.Aide
{
   public class PaintingFacade
    {
       protected static PaintingBLL paintingBLL = new PaintingBLL();
       public static dynamic Import(string excelFilePath)
       {
           FileStream fs = new FileStream(excelFilePath, FileMode.Open);
           DataTable paintingDataTable = NPOIExcel.RenderDataTableFromExcel(fs, 0, 0, true);
           fs.Close();
           dynamic result = new ExpandoObject();
           List<dynamic> paintingList = new List<dynamic>();
           foreach (DataRow dataRow in paintingDataTable.Rows)
           {
               dynamic painting = new ExpandoObject();
               painting.PaintingNumber = dataRow["PaintingNumber"].ToString();
               painting.Title = dataRow["Title"].ToString();
               painting.Authorships = StringNormalize.ReviseChineseComma(dataRow["Authorships"].ToString());
               painting.Subjects = StringNormalize.ReviseChineseComma(dataRow["Subjects"].ToString());
               painting.Tags = StringNormalize.ReviseChineseComma(dataRow["Tags"].ToString()).ToLower();
               painting.HandEnable = StringNormalize.IsTrueSynonym(dataRow["HandEnable"].ToString());
               painting.PrinterEnable = StringNormalize.IsTrueSynonym(dataRow["PrinterEnable"].ToString());
               painting.Frames = dataRow["Frames"].Equals(DBNull.Value) ? 0 : int.Parse(dataRow["Frames"].ToString());
               paintingList.Add(painting);
           }
           result.ErrorPaintingList = new List<dynamic>();
           foreach (dynamic painting in paintingList)
           {
               try
               {
                   paintingBLL.Import(painting);
               }
               catch (System.Exception ex)
               {
                   result.ErrorPaintingList.Add(painting);
               }
           }
           return result;
       }
       public static void Upload(string imageFloder, string ftpHost, string ftpUserName, string ftpPassword, string galleryFolder)
       {
           //打开本地文件夹
           //查找所有的图片文件
           //为每个图片插入一条Painting记录，然后上传图片
           DirectoryInfo folder = new DirectoryInfo(imageFloder);
           FileInfo[] fileInfos = folder.GetFiles("*.jpg", SearchOption.AllDirectories);
           foreach (FileInfo file in fileInfos)
           {
               if (file.Name.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
               {
                   string paintingNumber = file.Name.ToLower().Replace(".jpg", "");
                   int paintingId = paintingBLL.CreateSimplePainting(paintingNumber);
                   string folderPath = paintingBLL.GetVirtualFolderPath(paintingId);

                   //打开服务端
                   FTPClient ftpClient = new FTPClient(ftpHost, ftpUserName, ftpPassword);
                   string remoteFolderPath = galleryFolder + folderPath + "/";
                   ftpClient.CreateDirectory(remoteFolderPath);
                   Font font = new Font("Times New Roman", 24.0f);
                   string watermarkText = "www.1st-canvas-art.com";
                   Color frontColor = Color.White;
                   Stream stream = ImageWatermark.DrawImage(file.FullName, watermarkText, font, frontColor, WatermarkPosition.MiddleCenter);
                   //ftpClient.Upload(remoteFolderPath + file.Name, file.FullName);
                   ftpClient.Upload(remoteFolderPath + file.Name, stream);

                   //更新Painting.ImageUrl
                   dynamic painting = paintingBLL.GetPainting(paintingNumber);
                   painting.ImageUrl = "/Gallery" + folderPath + "/" + file.Name;
                   paintingBLL.Update(painting);
               }
           }
       }
    }
}
